<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/11/26
 * Time: 20:31
 */

namespace app\storage\model;

use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Model;



class StorageModel extends Model
{
    protected $table = 'iwh_storage';
    protected $pk = 'uuid';

    public function SupplierStorage()
    {
        return $this->hasOne('SupplierStorageModel', 'storage_uuid', 'uuid')->bind('supplier_uuid');
    }

    public function Supplier()
    {
        return $this->belongsToMany('SupplierModel', 'app\storage\model\SupplierStorageModel', 'supplier_uuid', 'storage_uuid');
    }

    public function Shelves()
    {
        return $this->hasMany('ShelvesModel', 'storage_uuid', 'uuid');
    }

    public function StaffStorage()
    {
        return $this->hasMany('StaffStorageModel', 'storage_uuid', 'uuid');
    }

    public function getStorage($condition)
    {
        $where = $condition['where'];
        $pageSize = $condition['pageSize'];
        $pageIndex = $condition['pageIndex'];

        $count = $this->alias('a')
            ->join(['iwh_supplier_storage'=>'b'],'a.uuid = b.storage_uuid','left')
            ->join(['iwh_supplier'=>'c'],'b.supplier_uuid = c.uuid','left')
            ->where($where)
            ->count();
        $result = $this->alias('a')->field('a.*,c.company_name')
            ->join(['iwh_supplier_storage'=>'b'],'a.uuid = b.storage_uuid','left')
            ->join(['iwh_supplier'=>'c'],'b.supplier_uuid = c.uuid','left')
            ->page($pageIndex,$pageSize)
            ->where($where)
            ->select();

        $ret = [
            'total' => $count,
            'row' => $result
        ];
        return $ret;
    }

    public function addStorage($data)
    {
        $data['uuid'] = create_uuid();
        $supplierData = ['uuid' => create_uuid(), 'supplier_uuid' => $data['supplier']];
        $this->setAttr('SupplierStorage', $supplierData);
        $result = $this->together('SupplierStorage')->allowField(true)->save($data);
        return $result;
    }

    public function deleteStorage($storageUuid)
    {
        $storageModel = StorageModel::get($storageUuid);
        $result = $storageModel->together('SupplierStorage')->delete();
        return $result;
    }

    public function editStorage($data)
    {
        $storageModel = StorageModel::get($data['uuid']);
        $storageModel->setAttr('SupplierStorage', ['supplier_uuid' => $data['supplier']]);
        $result = $storageModel->together('SupplierStorage')->isUpdate(true)->allowField(true)->save($data);
        return $result;
    }

    public function getStorageInfoByName($name){
        $a = 1;
        $storageInfo = $this->where('name',$name)->find();
        if(count($storageInfo)){
            return ret_template(false,"不存在仓库({$name})");
        }else{
            return ret_template(true,"查询成功",$storageInfo->toArray());
        }
    }

    public function checkStorage(array $where){
        $result = $this->where($where)->find();
        if(count($result)){
            return $result->toArray();
        }else{
            return [];
        }
    }

    /**返回仓库选择列表
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getStorageList(){
        $storageModel = new StorageModel();
        $storageList = $storageModel->field('uuid,name')->select();
        return count($storageList)? $storageList->toArray():[];
    }

    /**
     * @return mixed
     * 获取所有仓库的uuid
     * 确保admin能看到所有仓库
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public  static function getAllStorageUuid(){
        $result = Db::table('iwh_storage')->field('uuid')->select();
        if (!empty($result)){
            return array_column($result->toArray(),'uuid');

        }
    }
}

